package com.example.ruoyiakp.product.controller;


import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.ruoyiakp.product.domain.AgrAgriculturalDetect;
import com.example.ruoyiakp.product.domain.AgrAgriculturalDetectQrCode;
import com.example.ruoyiakp.product.service.AgrAgriculturalDetectQrCodeService;
import com.example.ruoyiakp.product.service.AgrAgriculturalDetectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * @ClassName AgrAgriculturalDetectController
 * @Description 质检API
 * @Author 陈萝瑶
 * @Date 2025/3/5 14:53
 */
@RestController
@RequestMapping("/detect")
@CrossOrigin(origins = "*")
public class AgrAgriculturalDetectController extends BaseController {
    @Autowired
    private AgrAgriculturalDetectService agrAgriculturalDetectService;

    @Autowired
    private AgrAgriculturalDetectQrCodeService agrAgriculturalDetectQrCodeService;

    private Long batchId;

    @RequiresPermissions("product:detect:list")
    @GetMapping("/list")
    public TableDataInfo list(AgrAgriculturalDetect agrAgriculturalDetect)
    {
        startPage();
        List<AgrAgriculturalDetect> list = agrAgriculturalDetectService.selectAgrAgriculturalDetectList(agrAgriculturalDetect);
        return getDataTable(list);
    }

    @RequiresPermissions("product:detect:export")
    @Log(title = "产品检测", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AgrAgriculturalDetect agrAgriculturalDetect)
    {
        List<AgrAgriculturalDetect> list = agrAgriculturalDetectService.selectAgrAgriculturalDetectList(agrAgriculturalDetect);
        ExcelUtil<AgrAgriculturalDetect> util = new ExcelUtil<AgrAgriculturalDetect>(AgrAgriculturalDetect.class);
        util.exportExcel(response, list, "产品检测数据");
    }

    @RequiresPermissions("product:detect:query")
    @GetMapping(value = "/{detectId}")
    public AjaxResult getInfo(@PathVariable("detectId") Long detectId)
    {
        return success(agrAgriculturalDetectService.selectAgrAgriculturalDetectByDetectId(detectId));
    }

    @Log(title = "产品检测", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody AgrAgriculturalDetect agrAgriculturalDetect)
    {
        return toAjax(agrAgriculturalDetectService.insertAgrAgriculturalDetect(agrAgriculturalDetect));
    }

    @PostMapping("/addBatch")
    public AjaxResult addBatch(@RequestBody Long batchId)
    {
        QueryWrapper<AgrAgriculturalDetectQrCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("batch_id",batchId);
        AgrAgriculturalDetectQrCode agrAgriculturalDetectQrCode = agrAgriculturalDetectQrCodeService.getOne(queryWrapper);
        if (agrAgriculturalDetectQrCode == null){
            return  toAjax(agrAgriculturalDetectService.insertDetectBatch(batchId));
        }else {

            return toAjax(agrAgriculturalDetectService.updateImg(batchId));
        }

    }

    @RequiresPermissions("product:detect:remove")
    @Log(title = "产品检测", businessType = BusinessType.DELETE)
    @DeleteMapping("/{detectIds}")
    public AjaxResult remove(@PathVariable Long[] detectIds)
    {
        AgrAgriculturalDetect agrAgriculturalDetect = agrAgriculturalDetectService.selectAgrAgriculturalDetectByDetectId(detectIds[0]);
        Long batchId = agrAgriculturalDetect.getBatchId();
        agrAgriculturalDetectService.deleteByBatchId(batchId);
        return toAjax(agrAgriculturalDetectService.deleteAgrAgriculturalDetectByDetectIds(detectIds));
    }

    @GetMapping(value = "/getBatch/{batchId}")
    public AjaxResult getBatch(@PathVariable("batchId") Long batchId)
    {
        this.batchId = batchId;
        return success(this.batchId);
    }

    @GetMapping(value = "/getBatchId")
    public AjaxResult getBatchId()
    {
        return success(this.batchId);
    }



}

